//
//  TKL_ForPhVC.m
//  TKL_EnPro
//
//  Created by Mrzhang on 2019/4/27.
//  Copyright © 2019年 MrZhang. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "TKL_ForPhVC.h"
#import "Head.h"
@interface TKL_ForPhVC ()<UITextFieldDelegate,UITextViewDelegate>
@property(nonatomic,strong)NSMutableDictionary* dataSource;

@end

@implementation TKL_ForPhVC
#pragma mark - getter and setter

-(NSMutableDictionary *)dataSource
{
    if (!_dataSource) {
        _dataSource=[NSMutableDictionary new];
    }
    return _dataSource;
}
#pragma mark - event
-(void)finishBtnPress
{
    [self.view endEditing:YES];
    
    if ([WFFunctions WFStrCheckEmpty:[self.dataSource objectForKey:@"name"]]) {
        [FTIndicator showInfoWithMessage:@"请填写姓名"];
        return;
    }
    if ([WFFunctions WFStrCheckEmpty:[self.dataSource objectForKey:@"phone"]]) {
        [FTIndicator showInfoWithMessage:@"请填写电话"];
        return;
    }
    if ([WFFunctions WFStrCheckEmpty:[self.dataSource objectForKey:@"time"]]) {
        [FTIndicator showInfoWithMessage:@"请选择预约时间"];
        return;
    }
    if ([WFFunctions WFStrCheckEmpty:[self.dataSource objectForKey:@"remark"]]) {
        [FTIndicator showInfoWithMessage:@"请填写维修内容"];
        return;
    }
    [FTIndicator showProgressWithMessage:@"正在保存"];
    BmobObject  *gameScore = [BmobObject objectWithClassName:@"for_book"];
    
    [gameScore setObject:[self.dataSource objectForKey:@"name"] forKey:@"name"];
    [gameScore setObject:[self.dataSource objectForKey:@"phone"] forKey:@"phone"];
    [gameScore setObject:[self.dataSource objectForKey:@"time"] forKey:@"time"];
    [gameScore setObject:[self.dataSource objectForKey:@"remark"] forKey:@"remark"];
   // [gameScore setObject:self.orderNum forKey:@"ordernum"];
   // [gameScore setObject:[RDLoginViewModel shareInterface].userID forKey:@"userid"];
    
    //异步保存到服务器
    [gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            //创建成功后会返回objectId，updatedAt，createdAt等信息
            //创建对象成功，打印对象值
            NSLog(@"%@",gameScore);
            [FTIndicator showSuccessWithMessage:@"预约成功,工作人员会尽快联系您"];
            [self.navigationController popViewControllerAnimated:YES];
        } else if (error){
            //发生错误后的动作
            NSLog(@"%@",error);
        } else {
            NSLog(@"Unknow error");
        }
    }];
}
#pragma mark - delegate
-(void)setcurrentTable
{
    @WeakSelf;
    self.baseTable.contentInset = UIEdgeInsetsMake([self topShelterHeight], 0, [self bottomShelterHeight], 0);
    self.view.backgroundColor = [UIColor whiteColor];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    
    
    [self setRightBarButtonWithTitle:@"提交" titleColor:HEXCOLOR(@"#333333") withBlock:^{
        [selfp finishBtnPress];
    }];
    [self.view addSubview:self.baseTable];
    
    [self viewModelBand];
    [self reloadData];
}
-(void)initilization
{
    self.isGroup = YES;
    [self setNavgationTheme:0];
    [self.navigationItem setTitle:@"预约拍摄"];
    self.extendedLayoutIncludesOpaqueBars = YES;
    [self setcurrentTable];
}
-(void)viewModelBand
{
    @WeakSelf;
}

-(void)reloadData
{
    [self requestGoodsData];
}
-(void)requestGoodsData
{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}
#pragma mark - delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    if (indexPath.row==3)
    {
        static NSString* identify=@"cell_00";
        UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell) {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor whiteColor];
        }
        [cell.contentView removeAllSubviews];
        
        UITextView* inputText=[[UITextView alloc] init];
        inputText.delegate=self;
        inputText.x=WFCGFloatX(15);
        inputText.y=WFCGFloatY(15);
        inputText.width=WFCGFloatX(345);
        inputText.height=WFCGFloatY(50);
        inputText.placeholder=@"请输入留言";
        inputText.font=SYSTEMFONT(15);
        [cell.contentView addSubview:inputText];
        inputText.text=[WFFunctions WFCheckEmptyBackStr:[self.dataSource objectForKey:@"remark"]];
        return cell;
    }
    static NSString* identify=@"ppPhonecel11l";
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor whiteColor];
    }
    [cell.contentView removeAllSubviews];
    NSArray* arr=@[@"姓名",@"电话",@"预约时间"];
    NSArray* placeArr=@[@"请填写姓名",@"请填写电话",@"选择预约时间"];
    NSArray* keyArr=@[@"name",@"phone",@"time"];
    UILabel* nameLab=[UILabel new];
    nameLab.x=WFCGFloatX(15);
    nameLab.y=WFCGFloatY(0);
    nameLab.width=WFCGFloatX(100);
    nameLab.height=WFCGFloatY(45);
    nameLab.textColor=HEXCOLOR(@"#333333");
    nameLab.font=SYSTEMFONT(15);
    [cell.contentView addSubview:nameLab];
    nameLab.text=arr[indexPath.row];
    
    
    UITextField* phoneTF = [[UITextField alloc]init];
    phoneTF.x=WFCGFloatX(120);
    phoneTF.y=WFCGFloatY(0);
    phoneTF.width=WFCGFloatX(240);
    phoneTF.height=WFCGFloatY(45);
    phoneTF.font = SYSTEMFONT(15);
    phoneTF.textColor = HEXCOLOR(@"#333333");
    phoneTF.placeholder=placeArr[indexPath.row];
    phoneTF.delegate = self;
    phoneTF.tag=900;
    phoneTF.textAlignment=NSTextAlignmentRight;
    [cell.contentView addSubview:phoneTF];
    if (indexPath.row==1) {
        phoneTF.keyboardType=UIKeyboardTypePhonePad;
    }else
    {
        phoneTF.keyboardType=UIKeyboardTypeDefault;
    }
    
    if (indexPath.row==2) {
        phoneTF.enabled=NO;
    }else
    {
        phoneTF.enabled=YES;
    }
    phoneTF.text=[WFFunctions WFCheckEmptyBackStr:[self.dataSource objectForKey:[keyArr objectAtIndex:indexPath.row]]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==3) {
        return WFCGFloatY(80);
    }
    return WFCGFloatY(45);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=[UIColor whiteColor];

    
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=HEXCOLOR(@"#F1F1F1");
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {

        if (indexPath.row==2)
        {
            [BRDatePickerView showDatePickerWithTitle:@"预约时间" dateType:UIDatePickerModeDateAndTime defaultSelValue:nil resultBlock:^(NSString *selectValue) {
                [self.dataSource setObject:selectValue forKey:@"time"];
                [self.baseTable reloadData];
            }];
        }
    }
    
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    [self.dataSource setObject:textView.text forKey:@"remark"];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
    
    for (int i=0; i<2; i++)
    {
        UITableViewCell* cell=[self.baseTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        UITextField* tf=(UITextField*)[cell.contentView viewWithTag:900];
        if (i==0&&textField==tf) {
            [self.dataSource setObject:textField.text forKey:@"name"];
        }else
        {
            [self.dataSource setObject:textField.text forKey:@"phone"];
        }
    }
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
